#include "OverTemperature.h"
#include "OverUnderX.h"
#include "../function_interface.h"
#include "../rated_limit.h"
#include "../types.h"
#include "../temp_sample.h"
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include "../logInterface.h"

static void IGBT_TempDiffCheck(int para)
/**
 * breif 温差检测（对比同相左右同点位探头温度差）
 */
{
    int16_t tempModuleLeft, tempModuleRight;
    uint16_t tempDiffThre;
    float tempDiff,tempLeft,tempRight;
    static u16BitType_t stateU16 = {0};
    static uint16_t HtdDiffFlag = HTD_FLAG_RESET;
    static int16_t tempModuleMax;

    FuncInf_GetArmThrshd(FUNCINF_ARM_THR_TYPE_TEMP_DIFF,&tempDiff);
    tempDiffThre = (uint16_t)tempDiff;

    //45度以下不进行温差检测
    TempSample_GetMaxTemperature(&tempModuleMax);
    if(tempModuleMax < 45)
    {
        printf("[%s]:igbt max temperature %d 度!\n",__FUNCTION__,tempModuleMax);

        if(HtdDiffFlag == HTD_FLAG_SET)
        {
            HtdDiffFlag = HTD_FLAG_RESET;
            RatedLimit_SetPercentage(RATED_LIMIT_OWNER_MODULE_TEMP_DIFF,1.0F);
            logInterfaceLogAdd(LOG_CLASS_SPECIAL, LOG_MAINTYPE_RATED_LIMIT, 201, COM_DATA_TYPE_NONE, NULL);
        }

        return;
    }

    //A
    FuncInf_GetIgbtTemperature(FUNCINF_IGBT_TEMP_TYPE_AL23,&tempLeft);
    FuncInf_GetIgbtTemperature(FUNCINF_IGBT_TEMP_TYPE_AR23,&tempRight);
    tempModuleLeft  = (int16_t)tempLeft;
    tempModuleRight = (int16_t)tempRight;
    if((abs(tempModuleLeft - tempModuleRight) > tempDiffThre) && 
        (stateU16.bitData.b0 == HTD_FLAG_RESET))
    {
        stateU16.bitData.b0 = HTD_FLAG_SET;
        logInterfaceLogAdd(LOG_CLASS_SPECIAL, LOG_MAINTYPE_MISC, 10, COM_DATA_TYPE_NONE, NULL);
        printf("[%s]: A相2/3采样点左右温差过大!\n", __FUNCTION__);
    }
    else if((abs(tempModuleLeft - tempModuleRight) < tempDiffThre - 2) && 
        (stateU16.bitData.b0 == HTD_FLAG_SET))
    {
        stateU16.bitData.b0 = HTD_FLAG_RESET;
        logInterfaceLogAdd(LOG_CLASS_SPECIAL, LOG_MAINTYPE_MISC, 11, COM_DATA_TYPE_NONE, NULL);
        printf("[%s]: A相2/3采样点左右温差恢复\n", __FUNCTION__);
    }

    FuncInf_GetIgbtTemperature(FUNCINF_IGBT_TEMP_TYPE_AL14,&tempLeft);
    FuncInf_GetIgbtTemperature(FUNCINF_IGBT_TEMP_TYPE_AR14,&tempRight);
    tempModuleLeft  = (int16_t)tempLeft;
    tempModuleRight = (int16_t)tempRight;
    if((abs(tempModuleLeft - tempModuleRight) > tempDiffThre) && 
        (stateU16.bitData.b1 == HTD_FLAG_RESET))
    {
        stateU16.bitData.b1 = HTD_FLAG_SET;
        logInterfaceLogAdd(LOG_CLASS_SPECIAL, LOG_MAINTYPE_MISC, 12, COM_DATA_TYPE_NONE, NULL);
        printf("[%s]: A相1/4采样点左右温差过大!\n", __FUNCTION__);
    }
    else if((abs(tempModuleLeft - tempModuleRight) < tempDiffThre - 2) && 
        (stateU16.bitData.b1 == HTD_FLAG_SET))
    {
        stateU16.bitData.b1 = HTD_FLAG_RESET;
        logInterfaceLogAdd(LOG_CLASS_SPECIAL, LOG_MAINTYPE_MISC, 13, COM_DATA_TYPE_NONE, NULL);
        printf("[%s]: A相1/4采样点左右温差恢复\n", __FUNCTION__);
    }
    //B
    FuncInf_GetIgbtTemperature(FUNCINF_IGBT_TEMP_TYPE_BL23,&tempLeft);
    FuncInf_GetIgbtTemperature(FUNCINF_IGBT_TEMP_TYPE_BR23,&tempRight);
    tempModuleLeft  = (int16_t)tempLeft;
    tempModuleRight = (int16_t)tempRight;
    if((abs(tempModuleLeft - tempModuleRight) > tempDiffThre) && 
        (stateU16.bitData.b2 == HTD_FLAG_RESET))
    {
        stateU16.bitData.b2 = HTD_FLAG_SET;
        logInterfaceLogAdd(LOG_CLASS_SPECIAL, LOG_MAINTYPE_MISC, 14, COM_DATA_TYPE_NONE, NULL);
        printf("[%s]: B相2/3采样点左右温差过大!\n", __FUNCTION__);
    }
    else if((abs(tempModuleLeft - tempModuleRight) < tempDiffThre - 2) && 
        (stateU16.bitData.b2 == HTD_FLAG_SET))
    {
        stateU16.bitData.b2 = HTD_FLAG_RESET;
        logInterfaceLogAdd(LOG_CLASS_SPECIAL, LOG_MAINTYPE_MISC, 15, COM_DATA_TYPE_NONE, NULL);
        printf("[%s]: B相2/3采样点左右温差恢复\n", __FUNCTION__);
    }

    FuncInf_GetIgbtTemperature(FUNCINF_IGBT_TEMP_TYPE_BL14,&tempLeft);
    FuncInf_GetIgbtTemperature(FUNCINF_IGBT_TEMP_TYPE_BR14,&tempRight);
    tempModuleLeft  = (int16_t)tempLeft;
    tempModuleRight = (int16_t)tempRight;
    if((abs(tempModuleLeft - tempModuleRight) > tempDiffThre) && 
        (stateU16.bitData.b3 == HTD_FLAG_RESET))
    {
        stateU16.bitData.b3 = HTD_FLAG_SET;
        logInterfaceLogAdd(LOG_CLASS_SPECIAL, LOG_MAINTYPE_MISC, 16, COM_DATA_TYPE_NONE, NULL);
        printf("[%s]: B相1/4采样点左右温差过大!\n", __FUNCTION__);
    }
    else if((abs(tempModuleLeft - tempModuleRight) < tempDiffThre - 2) && 
        (stateU16.bitData.b3 == HTD_FLAG_SET))
    {
        stateU16.bitData.b3 = HTD_FLAG_RESET;
        logInterfaceLogAdd(LOG_CLASS_SPECIAL, LOG_MAINTYPE_MISC, 17, COM_DATA_TYPE_NONE, NULL);
        printf("[%s]: B相1/4采样点左右温差恢复\n", __FUNCTION__);
    }
    //C
    FuncInf_GetIgbtTemperature(FUNCINF_IGBT_TEMP_TYPE_CL23,&tempLeft);
    FuncInf_GetIgbtTemperature(FUNCINF_IGBT_TEMP_TYPE_CR23,&tempRight);
    tempModuleLeft  = (int16_t)tempLeft;
    tempModuleRight = (int16_t)tempRight;
    if((abs(tempModuleLeft - tempModuleRight) > tempDiffThre) && 
        (stateU16.bitData.b4 == HTD_FLAG_RESET))
    {
        stateU16.bitData.b4 = HTD_FLAG_SET;
        logInterfaceLogAdd(LOG_CLASS_SPECIAL, LOG_MAINTYPE_MISC, 18, COM_DATA_TYPE_NONE, NULL);
        printf("[%s]: C相2/3采样点左右温差过大!\n", __FUNCTION__);
    }
    else if((abs(tempModuleLeft - tempModuleRight) < tempDiffThre - 2) && 
        (stateU16.bitData.b4 == HTD_FLAG_SET))
    {
        stateU16.bitData.b4 = HTD_FLAG_RESET;
        logInterfaceLogAdd(LOG_CLASS_SPECIAL, LOG_MAINTYPE_MISC, 19, COM_DATA_TYPE_NONE, NULL);
        printf("[%s]: C相2/3采样点左右温差恢复\n", __FUNCTION__);
    }

    FuncInf_GetIgbtTemperature(FUNCINF_IGBT_TEMP_TYPE_CL14,&tempLeft);
    FuncInf_GetIgbtTemperature(FUNCINF_IGBT_TEMP_TYPE_CR14,&tempRight);
    tempModuleLeft  = (int16_t)tempLeft;
    tempModuleRight = (int16_t)tempRight;
    if((abs(tempModuleLeft - tempModuleRight) > tempDiffThre) && 
        (stateU16.bitData.b5 == HTD_FLAG_RESET))
    {
        stateU16.bitData.b5 = HTD_FLAG_SET;
        logInterfaceLogAdd(LOG_CLASS_SPECIAL, LOG_MAINTYPE_MISC, 20, COM_DATA_TYPE_NONE, NULL);
        printf("[%s]: C相1/4采样点左右温差过大!\n", __FUNCTION__);
    }
    else if((abs(tempModuleLeft - tempModuleRight) < tempDiffThre - 2) && 
        (stateU16.bitData.b5 == HTD_FLAG_SET))
    {
        stateU16.bitData.b5 = HTD_FLAG_RESET;
        logInterfaceLogAdd(LOG_CLASS_SPECIAL, LOG_MAINTYPE_MISC, 21, COM_DATA_TYPE_NONE, NULL);
        printf("[%s]: C相1/4采样点左右温差恢复\n", __FUNCTION__);
    }

    if(stateU16.u16Data != HTD_FLAG_RESET)
    //出现温差过大，功率限半
    {
        if(HtdDiffFlag == HTD_FLAG_RESET)
        {
            HtdDiffFlag = HTD_FLAG_SET;
            RatedLimit_SetPercentage(RATED_LIMIT_OWNER_MODULE_TEMP_DIFF,0.5F);
            logInterfaceLogAdd(LOG_CLASS_SPECIAL, LOG_MAINTYPE_RATED_LIMIT, 200, COM_DATA_TYPE_NONE, NULL);
        }
        if(para)
        {
            printf("[%s]:igbt max temperature %d 度! IGBT TempDiff Start.Power limit half.\n",__FUNCTION__,tempModuleMax);
        }
    }
    else
    //温差正常，不限制功率
    {
        if(HtdDiffFlag == HTD_FLAG_SET)
        {
            HtdDiffFlag = HTD_FLAG_RESET;
            RatedLimit_SetPercentage(RATED_LIMIT_OWNER_MODULE_TEMP_DIFF,1.0F);
            logInterfaceLogAdd(LOG_CLASS_SPECIAL, LOG_MAINTYPE_RATED_LIMIT, 201, COM_DATA_TYPE_NONE, NULL);
        }
        if(para)
        {
            printf("[%s]:igbt max temperature %d 度! IGBT TempDiff Normal.Unlimited power.\n",__FUNCTION__,tempModuleMax);
        }
    }
}

static void IGBT_MaxTemp(int para)
/**
* 获取最大的igbt温度
*/
{
    int areaIndex/* 采样区域索引 */;
    int16_t maxTemp,minTemp,highTemp,temp;
    float highTempDerate,areaTemp;
    static u16BitType_t stateU16 = {0};

    FuncInf_GetArmThrshd(FUNCINF_ARM_THR_TYPE_HIG_TEMP_DERATE,&highTempDerate);
    highTemp = (int16_t)highTempDerate;

    for(areaIndex=0;areaIndex<TEMP_MONITOR_AREA_QTY;areaIndex++)
    {
        FuncInf_GetIgbtTemperature(areaIndex,&areaTemp);
        temp = (int16_t)areaTemp;
        if(0 == areaIndex)
        {
            maxTemp = temp;
            minTemp = temp;
        }
        else
        {
            if(temp > maxTemp)
            {
                maxTemp = temp;
            }
            else if(temp < minTemp)
            {
                minTemp = temp;
            }
        }
    }

    if((maxTemp > highTemp+5))
    //高温限零
    {
        if(stateU16.bitData.b0 == HTD_FLAG_RESET)
        {
            stateU16.bitData.b0 = HTD_FLAG_SET;
            stateU16.bitData.b1 = HTD_FLAG_RESET;

            RatedLimit_SetPercentage(RATED_LIMIT_OWNER_HIGH_TEMP_DERATE,0);

            logInterfaceLogAdd(LOG_CLASS_SPECIAL, LOG_MAINTYPE_MISC, 1, COM_DATA_TYPE_NONE, NULL);
            logInterfaceLogAdd(LOG_CLASS_SPECIAL, LOG_MAINTYPE_RATED_LIMIT, 202, COM_DATA_TYPE_NONE, NULL);
        }
        if(para)
        {
            printf("HTD Start,Power limit zero.Max IGBT temp:%d(1SheShiDu),derate High Temp:%d(1SheShiDu)\n",maxTemp,highTemp);
        }
    }
    else if((maxTemp > highTemp))
    //高温降额(降半)
    {
        if(stateU16.bitData.b1 == HTD_FLAG_RESET)
        {
            stateU16.bitData.b1 = HTD_FLAG_SET;
            stateU16.bitData.b0 = HTD_FLAG_RESET;

            RatedLimit_SetPercentage(RATED_LIMIT_OWNER_HIGH_TEMP_DERATE,0.5F);
            
            logInterfaceLogAdd(LOG_CLASS_SPECIAL, LOG_MAINTYPE_MISC, 0, COM_DATA_TYPE_NONE, NULL);
            logInterfaceLogAdd(LOG_CLASS_SPECIAL, LOG_MAINTYPE_RATED_LIMIT, 203, COM_DATA_TYPE_NONE, NULL);
        }
        if(para)
        {
            printf("HTD Start,Power limit half.Max IGBT temp:%d(1SheShiDu),derate High Temp:%d(1SheShiDu)\n",maxTemp,highTemp);
        }
    }
    else if((maxTemp < highTemp - 3))
    //高温降额结束
    {
        if(stateU16.bitData.b1 == HTD_FLAG_SET)
        {
            stateU16.bitData.b1 = HTD_FLAG_RESET;
            stateU16.bitData.b0 = HTD_FLAG_RESET;

            RatedLimit_SetPercentage(RATED_LIMIT_OWNER_HIGH_TEMP_DERATE,1.0F);
            logInterfaceLogAdd(LOG_CLASS_SPECIAL, LOG_MAINTYPE_RATED_LIMIT, 204, COM_DATA_TYPE_NONE, NULL);
        }
        if(para)
        {
            printf("HTD Stop,Unlimited power.Max IGBT temp:%d(1SheShiDu),derate High Temp:%d(1SheShiDu)\n",maxTemp,highTemp);
        }
    }
}

void Other_MaxTemp(int para)
{
    float temp=0,temp1,temp2;
    static u16BitType_t stateU16 = {0};

    FuncInf_GetIsoBoardData(FUNCINF_ISO_BOARD_DATA_TYPE_TEMP_REACT,&temp);
    if(temp > 130 && temp <= 150)
    {
        if(stateU16.bitData.b0 == HTD_FLAG_RESET)
        {
            stateU16.bitData.b0 = HTD_FLAG_SET;
            stateU16.bitData.b1 = HTD_FLAG_RESET;
            RatedLimit_SetPercentage(RATED_LIMIT_OWNER_REACTOR_OVER_TEMP,0);
            logInterfaceLogAdd(LOG_CLASS_SPECIAL, LOG_MAINTYPE_MISC, 0, COM_DATA_TYPE_NONE, NULL);
            logInterfaceLogAdd(LOG_CLASS_SPECIAL, LOG_MAINTYPE_RATED_LIMIT, 205, COM_DATA_TYPE_NONE, NULL);
        }

        printf("电抗器温度:%.2f 处于(130, 150]度：待机（封脉冲）！\n",temp);
    }
    else if(temp > 150)
    {
        if(stateU16.bitData.b1 == HTD_FLAG_RESET)
        {
            stateU16.bitData.b1 = HTD_FLAG_SET;
            stateU16.bitData.b0 = HTD_FLAG_RESET;
            RatedLimit_SetPercentage(RATED_LIMIT_OWNER_REACTOR_OVER_TEMP,0);
            FuncInf_TurnOnOff(FUNCINF_OPERATOR_OVER_TEMP,NULL,FUNCINF_TURN_OFF);
            logInterfaceLogAdd(LOG_CLASS_SPECIAL, LOG_MAINTYPE_MISC, 1, COM_DATA_TYPE_NONE, NULL);
            logInterfaceLogAdd(LOG_CLASS_SPECIAL, LOG_MAINTYPE_RATED_LIMIT, 205, COM_DATA_TYPE_NONE, NULL);
        }

        printf("电抗器温度:%.2f 高于150度: 停机（禁止开机）！\n",temp);
    }
    else
    {
        if(stateU16.bitData.b0 == HTD_FLAG_SET)
        {
            stateU16.bitData.b0 = HTD_FLAG_RESET;
            stateU16.bitData.b1 = HTD_FLAG_RESET;
            RatedLimit_SetPercentage(RATED_LIMIT_OWNER_REACTOR_OVER_TEMP,1.0F);
            logInterfaceLogAdd(LOG_CLASS_SPECIAL, LOG_MAINTYPE_RATED_LIMIT, 206, COM_DATA_TYPE_NONE, NULL);
        }
        if(para)
        {
            printf("电抗器温度:%.2f 小于130度, 温度正常！\n",temp);
        }
    }

    FuncInf_GetIsoBoardData(FUNCINF_ISO_BOARD_DATA_TYPE_TEMP_BUSCAP,&temp);
    if(temp > 70)
    {
        if(stateU16.bitData.b2 == HTD_FLAG_RESET)
        {
            stateU16.bitData.b2 = HTD_FLAG_SET;
            RatedLimit_SetPercentage(RATED_LIMIT_OWNER_BUS_CAP_OVER_TEMP,0);
            logInterfaceLogAdd(LOG_CLASS_SPECIAL, LOG_MAINTYPE_MISC, 0, COM_DATA_TYPE_NONE, NULL);
            logInterfaceLogAdd(LOG_CLASS_SPECIAL, LOG_MAINTYPE_RATED_LIMIT, 207, COM_DATA_TYPE_NONE, NULL);
        }
        printf("母排电容温度:%.2f 高于70度: 待机（封脉冲）！\n",temp);
    }
    else
    {
        if(stateU16.bitData.b2 == HTD_FLAG_SET)
        {
            stateU16.bitData.b2 = HTD_FLAG_RESET;
            RatedLimit_SetPercentage(RATED_LIMIT_OWNER_BUS_CAP_OVER_TEMP,1.0F);
            logInterfaceLogAdd(LOG_CLASS_SPECIAL, LOG_MAINTYPE_RATED_LIMIT, 208, COM_DATA_TYPE_NONE, NULL);
        }
        if(para)
        {
            printf("母排电容温度:%.2f 小于70度: 温度正常！\n",temp);
        }
    }

    FuncInf_GetIsoBoardData(FUNCINF_ISO_BOARD_DATA_TYPE_TEMP_FLTCAP1,&temp1);
    FuncInf_GetIsoBoardData(FUNCINF_ISO_BOARD_DATA_TYPE_TEMP_FLTCAP2,&temp2);
    temp = (temp1>temp2)?(temp1):(temp2);
    if(temp > 65)
    {
        if(stateU16.bitData.b3 == HTD_FLAG_RESET)
        {
            stateU16.bitData.b3 = HTD_FLAG_SET;
            RatedLimit_SetPercentage(RATED_LIMIT_OWNER_FILTER_CAP_OVER_TEMP,0);
            logInterfaceLogAdd(LOG_CLASS_SPECIAL, LOG_MAINTYPE_MISC, 0, COM_DATA_TYPE_NONE, NULL);
            logInterfaceLogAdd(LOG_CLASS_SPECIAL, LOG_MAINTYPE_RATED_LIMIT, 209, COM_DATA_TYPE_NONE, NULL);
        }

        printf("滤波电容温度:%.2f 高于65度: 待机（封脉冲）！\n",temp);
    }
    else
    {
        if(stateU16.bitData.b3 == HTD_FLAG_SET)
        {
            stateU16.bitData.b3 = HTD_FLAG_RESET;
            RatedLimit_SetPercentage(RATED_LIMIT_OWNER_FILTER_CAP_OVER_TEMP,1.0F);
            logInterfaceLogAdd(LOG_CLASS_SPECIAL, LOG_MAINTYPE_RATED_LIMIT, 210, COM_DATA_TYPE_NONE, NULL);
        }
        if(para)
        {
            printf("滤波电容温度:%.2f 小于65度: 温度正常！\n",temp);
        }
    }

    FuncInf_GetIsoBoardData(FUNCINF_ISO_BOARD_DATA_TYPE_TEMP_ACCBNT,&temp);
    if(temp > 70)
    {
        if(stateU16.bitData.b4 == HTD_FLAG_RESET)
        {
            stateU16.bitData.b4 = HTD_FLAG_SET;
            RatedLimit_SetPercentage(RATED_LIMIT_OWNER_AC_CABNET_OVER_TEMP,0);
            logInterfaceLogAdd(LOG_CLASS_SPECIAL, LOG_MAINTYPE_MISC, 0, COM_DATA_TYPE_NONE, NULL);
            logInterfaceLogAdd(LOG_CLASS_SPECIAL, LOG_MAINTYPE_RATED_LIMIT, 211, COM_DATA_TYPE_NONE, NULL);
        }

        printf("交流柜温度:%.2f 高于70度: 待机（封脉冲）！\n",temp);
    }
    else
    {
        if(stateU16.bitData.b4 == HTD_FLAG_SET)
        {
            stateU16.bitData.b4 = HTD_FLAG_RESET;
            RatedLimit_SetPercentage(RATED_LIMIT_OWNER_AC_CABNET_OVER_TEMP,1.0F);
            logInterfaceLogAdd(LOG_CLASS_SPECIAL, LOG_MAINTYPE_RATED_LIMIT, 212, COM_DATA_TYPE_NONE, NULL);
        }
        if(para)
        {
            printf("交流柜温度:%.2f 小于70度: 温度正常！\n",temp);
        }
    }

}

void OverTemp_1SecondPerLoop(const void* pPara)
//过温检测,1s一周期调用
{
    int para = (int)pPara;
    IGBT_TempDiffCheck(para);
    IGBT_MaxTemp(para);
    Other_MaxTemp(para);
}